package com.hyacinth.lotterysystem.service.activityStatus.operater;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyacinth.lotterysystem.dao.entity.ActivityUserDO;
import com.hyacinth.lotterysystem.dao.mapper.ActivityUserMapper;
import com.hyacinth.lotterysystem.service.activityStatus.ConvertActivityStatusDTO;
import com.hyacinth.lotterysystem.service.enums.ActivityUserStatusEnum;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator {
    private static final Logger logger = LoggerFactory.getLogger(PrizeOperator.class);

    @Resource
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum userStatusEnum = convertActivityStatusDTO.getUserStatusEnum();
        if (CollectionUtils.isEmpty(userIds) || activityId == null || userStatusEnum == null)
            return false;
        LambdaQueryWrapper<ActivityUserDO> activityUserDOLambdaQueryWrapper = Wrappers.lambdaQuery(ActivityUserDO.class)
                .eq(ActivityUserDO::getActivityId, activityId)
                .in(ActivityUserDO::getUserId, userIds);
        List<ActivityUserDO> activityUserDOS = activityUserMapper.selectList(activityUserDOLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(activityUserDOS))
            return false;
        for (ActivityUserDO activityUserDO : activityUserDOS) {
            if (activityUserDO.getStatus().equalsIgnoreCase(userStatusEnum.name()))
                return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum userStatusEnum = convertActivityStatusDTO.getUserStatusEnum();
        LambdaUpdateWrapper<ActivityUserDO> activityUserDOLambdaUpdateWrapper = Wrappers.lambdaUpdate(ActivityUserDO.class)
                .eq(ActivityUserDO::getActivityId, activityId)
                .in(ActivityUserDO::getUserId, userIds)
                .set(ActivityUserDO::getStatus, userStatusEnum.name());
        activityUserMapper.update(activityUserDOLambdaUpdateWrapper);
        logger.info("activityUser 活动人员状态扭转了");
        return true;
    }
}
